package org.budo.activiti.form.type;

import org.activiti.engine.form.AbstractFormType;
import org.apache.commons.lang3.StringUtils;
import org.budo.time.Time;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

/**
 * 日期范围
 * 
 * @author lmw
 */
public class TimeRangeFormType extends AbstractFormType {
    private static final long serialVersionUID = -1611430110626973742L;

    public static final String NAME = "time_range";

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public Object convertFormValueToModelValue(String propertyValue) {
        if (StringUtils.isEmpty(propertyValue)) {
            return null;
        }

        JSONArray jsonArray = JSON.parseArray(propertyValue);

        String from = Time.when(jsonArray.get(0)) //
                .plusHour(TimeFormType.TIME_ZONE) //
                .toString(TimeFormType.FORMAT);

        String to = Time.when(jsonArray.get(1)) //
                .plusHour(TimeFormType.TIME_ZONE) //
                .toString(TimeFormType.FORMAT);

        return "[\"" + from + "\", \"" + to + "\"]";
    }

    @Override
    public String convertModelValueToFormValue(Object modelValue) {
        throw new RuntimeException();
    }
}